package com.hgvip.service.impl;

import com.hgvip.service.GoodsService;
import com.hgvip.util.KaoLaUtil;
import com.hgvip.util.SignUtil;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by arnold.zhu on 2017/9/20.
 */
@Service("GoodsService")
public class GoodsServiceImpl implements GoodsService {

    private Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Value("${kaola.url}")
    private String url;

    @Value("${kaola.appKey}")
    private String appKey;

    @Value("${kaola.secretKey}")
    private String secretKey;

    @Value("${kaola.channelId}")
    private String channelId;

    private String sign_method = "md5";

    @Override
    public String queryAllGoodsId() {
        String requestUrl = url + "/queryAllGoodsId";

        TreeMap<String, String> parameterMap = createGeneralTreeMap();

        String sign = SignUtil.createSign(secretKey, parameterMap);

        List<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("timestamp", parameterMap.get("timestamp")));
        params.add(new BasicNameValuePair("v", "1.0"));
        params.add(new BasicNameValuePair("sign_method", sign_method));
        params.add(new BasicNameValuePair("app_key", appKey));
        params.add(new BasicNameValuePair("sign", sign));
        params.add(new BasicNameValuePair("channelId", channelId));

        String allGoodsId = KaoLaUtil.sendRequest(requestUrl, params);

        return allGoodsId;
    }

    @Override
    public String queryGoodsInfoByIds(String skuIds, String queryType, String channelSalePrices) {
        logger.info("被调用了");
        String requestUrl = url + "/queryGoodsInfoByIds";

        TreeMap<String, String> parameterMap = createGeneralTreeMap();
        parameterMap.put("skuIds", skuIds);
        parameterMap.put("queryType", queryType);
        if (!StringUtils.isEmpty(channelSalePrices)) {
            parameterMap.put("channelSalePrices", channelSalePrices);
        }

        String sign = SignUtil.createSign(secretKey, parameterMap);

        List<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("timestamp", parameterMap.get("timestamp")));
        params.add(new BasicNameValuePair("v", "1.0"));
        params.add(new BasicNameValuePair("sign_method", sign_method));
        params.add(new BasicNameValuePair("app_key", appKey));
        params.add(new BasicNameValuePair("sign", sign));
        params.add(new BasicNameValuePair("channelId", channelId));
        params.add(new BasicNameValuePair("skuIds", skuIds));
        params.add(new BasicNameValuePair("queryType", queryType));

        if (!StringUtils.isEmpty(channelSalePrices)) {
            params.add(new BasicNameValuePair("channelSalePrices", channelSalePrices));
        }

        String allGoodsInfo = KaoLaUtil.sendRequest(requestUrl, params);

        return allGoodsInfo;
    }

    private TreeMap<String, String> createGeneralTreeMap() {
        long time = System.currentTimeMillis();
        TreeMap<String, String> parameterMap = new TreeMap<>();
        parameterMap.put("timestamp", new Timestamp(time).toString());
        parameterMap.put("v", "1.0");
        parameterMap.put("sign_method", sign_method);
        parameterMap.put("app_key", appKey);
        parameterMap.put("channelId", channelId);
        return parameterMap;
    }
}
